#include<iostream>
#include<cstring>
using namespace std;

long long cantor(int a[], int n) {
    int f[15];
	long long answer = 0;
    bool v[15];
    memset(f, 0, sizeof(f));
    memset(v, false, sizeof(v));
    f[1] = 1;
    for(int i=2; i<=n; i++)  f[i] = f[i-1] * i;
    for(int i=0; i<n; i++) {
        long long count = 0;
        for(int j=1; j<a[i]; j++) {
            if(!v[j])   count++;
        }
        answer += count * f[n-i-1];
        v[a[i]] = true;
    }
    return answer + 1;
}

void reverse_cantor(int mod, int n) {
    int a[15] = {0}, f[15], count, index=0;
    bool v[15] = {false};
    f[0] = 1;
    mod--;
    for(int i=1; i<=n; i++)  f[i] = f[i-1] * i;
    for (int i=0; i<n; i++) {
        count = mod / f[n-i-1];
        mod %= f[n-i-1];
        for(int j=1; j<=n; j++) {
			if(v[j] == false) {
				if(count == 0) {
					a[index++] = j;
	                v[j] = true;
	                break;
				} else count--;
			}
        }
    }
    for (int i=0; i<n; i++) {
        cout << a[i] << " ";
    }
}

int main() {
    int n, k, a[9];
    cin >> n;
    for(int i=0; i<n; i++)
        cin >> a[i];
    cin >> k;
    cout << cantor(a, n) << endl;
    reverse_cantor(k, n);
    return 0;
}
